import { assign, createActor, setup } from 'xstate'

export const rootMachine = setup({
  types: {
    events: {} as { type: 'START' } | { type: 'CHANGE' }
  }
}).createMachine({
  context: {
    times: 0
  },
  id: 'root',
  initial: 'idle',
  states: {
    idle: {
      on: {
        START: {
          target: 'green',
        }
      }
    },
    green: {
      entry: assign({
        times: ({context}) => context.times + 1
      }),
      on: {
        CHANGE: 'red',
      },
    },
    red: {
      on: {
        CHANGE: 'green',
      },
    }
  }
})

export const rootActor = createActor(rootMachine).start()

rootActor.send